import { userStore } from '@/store/modules/user'
import request from '../utils/request'

// 用户登录
export const login = (data: any) =>
    request('/pub/login', data, 'POST', {
        header: {
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        },
    })

// 获取用户权限及用户信息
export const getRight = () =>
    new Promise(resolve => {
        request('/cli/bus/client/get_user_modules.do')
            .then((rightData: any) => {
                const { data } = rightData
                // 找到电子入库单的权限并拼成数组
                const right = data.find((item: AnyObject) => item.moduleId === '09')?.children[0].children
                const list = right ? right.map((item: AnyObject) => item.moduleId) : null
                // 存储权限信息
                userStore().setRight(list)
            })
            .then(() => {
                request('/cli/bus/client/get_user.do').then((personalData: any) => {
                    const { data } = personalData
                    // 存储个人信息
                    userStore().setUserInfo(data)
                    resolve(() => {})
                })
            })
    })

//注销
export const logout = () => request('/cli/bus/client/logout')
